﻿using EasyLinkerSDK.Common.Json;
using EasyLinkerSDK.Enum;
using EasyLinkerSDK.Model.Server.Common.Interface;
using EasyLinkerSDK.Model.Server.Common.ServerMessage;
using EasyLinkerSDK.Model.Server.Control;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace EasyLinkerSDK.Client.Message
{
    class MessageProcessor
    {
        public IServerMessage BuildControlMessage(ServerMessage serverMessage)
        {
            if (serverMessage == null || string.IsNullOrEmpty(serverMessage.Data.Content) || serverMessage.Type != MessageType.Control)
            {
                return null;
            }
            IServerMessage messageObj = null;
            switch (serverMessage.Action)
            {
                case ActionName.AdjustUpBrightness:
                case ActionName.AdjustDownBrightness:
                    messageObj = JsonHelper.DeserializeJsonToObject<AdjustBrightness>(serverMessage.Data.Content);
                    break;
                case ActionName.AdjustDownVolume:
                case ActionName.AdjustUpVolume:
                case ActionName.SetVolume:
                case ActionName.SetMute:
                case ActionName.CancelMute:
                    messageObj = JsonHelper.DeserializeJsonToObject<SetVolume>(serverMessage.Data.Content);
                    break;
                case ActionName.Cancel:
                    messageObj = JsonHelper.DeserializeJsonToObject<Cancel>(serverMessage.Data.Content);
                    break;
                case ActionName.CloseSwing:
                    messageObj = JsonHelper.DeserializeJsonToObject<SwingControl>(serverMessage.Data.Content);
                    break;
                case ActionName.Continue:
                    messageObj = JsonHelper.DeserializeJsonToObject<Continue>(serverMessage.Data.Content);
                    break;
                case ActionName.Custom:
                    messageObj = JsonHelper.DeserializeJsonToObject<Custom>(serverMessage.Data.Content);
                    break;
                case ActionName.FastForward:
                    messageObj = JsonHelper.DeserializeJsonToObject<FastForward>(serverMessage.Data.Content);
                    break;
                case ActionName.CloseFunction:
                case ActionName.OpenFunction:
                    messageObj = JsonHelper.DeserializeJsonToObject<Function>(serverMessage.Data.Content);
                    break;
                case ActionName.SetMode:
                case ActionName.CancelMode:
                    messageObj = JsonHelper.DeserializeJsonToObject<Mode>(serverMessage.Data.Content);
                    break;
                case ActionName.Next:
                    messageObj = JsonHelper.DeserializeJsonToObject<Next>(serverMessage.Data.Content);
                    break;
                case ActionName.OpenSwing:
                    messageObj = JsonHelper.DeserializeJsonToObject<SwingControl>(serverMessage.Data.Content);
                    break;
                case ActionName.Pause:
                    messageObj = JsonHelper.DeserializeJsonToObject<Pause>(serverMessage.Data.Content);
                    break;
                case ActionName.Play:
                    messageObj = JsonHelper.DeserializeJsonToObject<Play>(serverMessage.Data.Content);
                    break;
                case ActionName.Previous:
                    messageObj = JsonHelper.DeserializeJsonToObject<Previous>(serverMessage.Data.Content);
                    break;
                case ActionName.SelectChannel:
                case ActionName.AdjustUpChannel:
                case ActionName.AdjustDownChannel:
                    messageObj = JsonHelper.DeserializeJsonToObject<SelectChannel>(serverMessage.Data.Content);
                    break;
                case ActionName.SetBrightness:
                    messageObj = JsonHelper.DeserializeJsonToObject<SetBrightness>(serverMessage.Data.Content);
                    break;
                case ActionName.SetColor:
                    messageObj = JsonHelper.DeserializeJsonToObject<SetColor>(serverMessage.Data.Content);
                    break;
                case ActionName.SetColorTemperature:
                case ActionName.AdjustDownColorTemperature:
                case ActionName.AdjustUpColorTemperature:
                    messageObj = JsonHelper.DeserializeJsonToObject<SetColorTemperature>(serverMessage.Data.Content);
                    break;
                case ActionName.SetHumidity:
                case ActionName.AdjustUpHumidity:
                case ActionName.AdjustDownHumidity:
                    messageObj = JsonHelper.DeserializeJsonToObject<SetHumidity>(serverMessage.Data.Content);
                    break;
                case ActionName.SetTemperature:
                case ActionName.AdjustUpTemperature:
                case ActionName.AdjustDownTemperature:
                    messageObj = JsonHelper.DeserializeJsonToObject<SetTemperature>(serverMessage.Data.Content);
                    break;
                case ActionName.SetWindSpeed:
                case ActionName.AdjustUpWindSpeed:
                case ActionName.AdjustDownWindSpeed:
                    messageObj = JsonHelper.DeserializeJsonToObject<SetWindSpeed>(serverMessage.Data.Content);
                    break;
                case ActionName.OpenUpAndDownSwing:
                case ActionName.OpenUpSwing:
                case ActionName.OpenDownSwing:
                case ActionName.OpenLeftAndRightSwing:
                case ActionName.OpenLeftSwing:
                case ActionName.OpenRightSwing:
                case ActionName.OpenForwardAndBackSwing:
                case ActionName.OpenForwardSwing:
                case ActionName.OpenBackSwing:
                    messageObj = JsonHelper.DeserializeJsonToObject<Model.Server.Control.SwingAngle>(serverMessage.Data.Content);
                    break;
                case ActionName.TurnOff:
                    messageObj = JsonHelper.DeserializeJsonToObject<PowerControl>(serverMessage.Data.Content);
                    break;
                case ActionName.TurnOn:
                    messageObj = JsonHelper.DeserializeJsonToObject<PowerControl>(serverMessage.Data.Content);
                    break;
                default:
                    break;
            }
            return messageObj;
        }

    }
}
